module.exports = class MeshLine {
    constructor() {
        this.positions = [];
    
        this.previous = [];
        this.next = [];
        this.side = [];
        this.width = [];
        this.indices_array = [];
        this.uvs = [];
        this.counters = [];
    
        this.widthCallback = null;
    }

    setGeometry(g, s, color, opacity, lineWidth, c) {
        this.widthCallback = c;
        this.startSeq = s;
        this.color = color;
        this.opacity = opacity;
        this.lineWidth = lineWidth;

        this.positions = [];
        this.seqs = [];
        this.counters = [];

        if (g instanceof Float32Array || g instanceof Array) {
            for (var j = 0; j < g.length; j += 3) {
                var c = j / g.length;
                this.positions.push(g[j], g[j + 1], g[j + 2]);
                this.positions.push(g[j], g[j + 1], g[j + 2]);
                this.counters.push(c);
                this.counters.push(c);
            }
        }
        this.process();
    }

    compareV3(a, b) {
        var aa = a * 6;
        var ab = b * 6;
        return (this.positions[aa] === this.positions[ab]) && (this.positions[aa + 1] === this.positions[ab + 1]) && (this.positions[aa + 2] === this.positions[ab + 2]);
    }

    copyV3(a) {
        var aa = a * 6;
        return [this.positions[aa], this.positions[aa + 1], this.positions[aa + 2]];
    }

    process() {
        var l = this.positions.length / 6;

        this.previous = [];
        this.next = [];
        this.side = [];
        this.width = [];
        this.indices_array = [];
        this.uvs = [];
        this.colors = [];
        this.opacitys = [];
        this.lineWidths = [];

        for (var j = 0; j < l; j++) {
            this.side.push(1);
            this.side.push(-1);
        }

        var w;
        for (var j = 0; j < l; j++) {
            if (this.widthCallback) w = this.widthCallback(j / (l - 1));
            else w = 1;
            this.width.push(w);
            this.width.push(w);
            this.seqs.push(this.startSeq + j);
            this.seqs.push(this.startSeq + j);

            if (this.color) {
                this.colors.push(this.color[0]);
                this.colors.push(this.color[1]);
                this.colors.push(this.color[2]);
                this.colors.push(this.color[0]);
                this.colors.push(this.color[1]);
                this.colors.push(this.color[2]);
            }

            if (this.opacitys) {
                this.opacitys.push(this.opacity);
                this.opacitys.push(this.opacity);
            }

            if (this.lineWidths) {
                this.lineWidths.push(this.lineWidth);
                this.lineWidths.push(this.lineWidth);
            }
        }

        for (var j = 0; j < l; j++) {
            this.uvs.push(j / (l - 1), 0);
            this.uvs.push(j / (l - 1), 1);
        }

        var v;

        if (this.compareV3(0, l - 1)) {
            v = this.copyV3(l - 2);
        } else {
            v = this.copyV3(0);
        }
        this.previous.push(v[0], v[1], v[2]);
        this.previous.push(v[0], v[1], v[2]);
        for (var j = 0; j < l - 1; j++) {
            v = this.copyV3(j);
            this.previous.push(v[0], v[1], v[2]);
            this.previous.push(v[0], v[1], v[2]);
        }

        for (var j = 1; j < l; j++) {
            v = this.copyV3(j);
            this.next.push(v[0], v[1], v[2]);
            this.next.push(v[0], v[1], v[2]);
        }

        if (this.compareV3(l - 1, 0)) {
            v = this.copyV3(1);
        } else {
            v = this.copyV3(l - 1);
        }
        this.next.push(v[0], v[1], v[2]);
        this.next.push(v[0], v[1], v[2]);

        for (var j = 0; j < l - 1; j++) {
            var n = j * 2;
            this.indices_array.push(n, n + 1, n + 2);
            this.indices_array.push(n + 2, n + 1, n + 3);
        }

        this.grometry = {
            seq: this.seqs,
            position: this.positions,
            previous: this.previous,
            next: this.next,
            side: this.side,
            width: this.width,
            colors: this.colors,
            opacitys: this.opacitys,
            lineWidths: this.lineWidths,
            index: this.indices_array,
            counters: this.counters,
            n: this.indices_array.length
        }
    }
}
